// src/main/java/org/mms/service/impl/OutboundServiceImpl.java
package org.mms.service.impl;

import org.mms.exception.BusinessException;
import org.mms.pojo.entity.Material;
import org.mms.pojo.entity.MaterialOutbound;
import org.mms.pojo.entity.User;
import org.mms.pojo.entity.Warehouse;
import org.mms.pojo.request.MaterialOutboundRequest;
import org.mms.pojo.request.MaterialOutboundReviewRequest;
import org.mms.repository.MaterialRepository;
import org.mms.repository.OutboundRepository;
import org.mms.repository.UserRepository;
import org.mms.service.OutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OutboundServiceImpl implements OutboundService {
    @Autowired
    private OutboundRepository outboundRepository;
    @Autowired
    private MaterialRepository materialRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public List<MaterialOutbound> list() {
        return outboundRepository.findAll();
    }

    @Override
    public MaterialOutbound getById(Long id) {
        return outboundRepository.findById(id)
                .orElseThrow(() -> new BusinessException("出库记录不存在"));
    }

    @Override
    public MaterialOutbound create(MaterialOutboundRequest request) {
        MaterialOutbound entity = new MaterialOutbound();

        Material material = materialRepository.findById(request.getMaterialId())
                .orElseThrow(() -> new BusinessException("物资不存在"));

        User operator = userRepository.findById(request.getOperatorId())
                .orElseThrow(() -> new BusinessException("操作人不存在"));

        if (material.getQuantity() < request.getQuantity()) {
            throw new BusinessException("物资数量不足，无法出库");
        }

        entity.setOutboundNumber(request.getOutboundNumber());
        entity.setMaterial(material);
        entity.setQuantity(request.getQuantity());
        entity.setOperator(operator);
        entity.setReviewer(null);
        entity.setStatus("待审核");
        return outboundRepository.save(entity);
    }

    @Override
    public MaterialOutbound update(MaterialOutboundRequest request) {
        MaterialOutbound entity = outboundRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("出库记录不存在"));

        if (!"待审核".equals(entity.getStatus())) {
            throw new BusinessException("只能编辑待审核状态的记录");
        }

        Material material = materialRepository.findById(request.getMaterialId())
                .orElseThrow(() -> new BusinessException("物资不存在"));

        User operator = userRepository.findById(request.getOperatorId())
                .orElseThrow(() -> new BusinessException("操作人不存在"));

        if (material.getQuantity() < request.getQuantity()) {
            throw new BusinessException("物资数量不足，无法出库");
        }
        entity.setId(request.getId());
        entity.setOutboundNumber(entity.getOutboundNumber());
        entity.setMaterial(material);
        entity.setQuantity(request.getQuantity());
        entity.setOperator(operator);
        entity.setStatus(request.getStatus());
        return outboundRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        MaterialOutbound entity = outboundRepository.findById(id)
                .orElseThrow(() -> new BusinessException("出库记录不存在"));
        if (!"待审核".equals(entity.getStatus())) {
            throw new BusinessException("只能删除待审核状态的记录");
        }
        outboundRepository.deleteById(id);
    }

    @Override
    public MaterialOutbound review(MaterialOutboundReviewRequest request) {
        MaterialOutbound entity = outboundRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("出库记录不存在"));
        if (!"待审核".equals(entity.getStatus())) {
            throw new BusinessException("只能审核待审核状态的记录");
        }
        User reviewer = userRepository.findById(request.getReviewerId())
                .orElseThrow(() -> new BusinessException("审核人不存在"));
        if ("通过".equals(request.getStatus())) {
            Material material = entity.getMaterial();
            if (material.getQuantity() < entity.getQuantity()) {
                throw new BusinessException("物资数量不足，无法通过审核");
            }
            material.setQuantity(material.getQuantity() - entity.getQuantity());
            materialRepository.save(material);
        }
        entity.setStatus(request.getStatus());
        entity.setReviewer(reviewer);
        return outboundRepository.save(entity);
    }
}